package cn.vehicle.biz.eventListener;

import cn.vehicle.biz.event.GIoInResultEvent;
import cn.vehicle.constants.Constants;
import cn.vehicle.domains.ctx.PlateDetectContext;
import cn.vehicle.domains.entity.CameraEntity;
import cn.vehicle.enums.GIoInTypeEnum;
import cn.vehicle.enums.VehicleSnapshotTypeEnum;
import cn.vehicle.enums.VehicleTypeEnum;
import cn.vehicle.exception.VehicleAccessException;
import cn.vehicle.service.CameraService;
import cn.vehicle.service.CameraSnapshotService;
import cn.vehicle.service.VehicleTransportLedgerService;
import cn.vehicle.utils.Base64ToImageUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class GIoInResultHeadMidTailSnapshotListener implements ApplicationListener<GIoInResultEvent> {

    @Resource
    private CameraSnapshotService cameraSnapshotService;

    @Resource
    private CameraService cameraService;

    @Resource
    private VehicleTransportLedgerService vehicleTransportLedgerService;

    ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(2);

    @Override
    public void onApplicationEvent(GIoInResultEvent event) {
        //前中后3长照片处理
        PlateDetectContext plateDetectContext = event.getPlateDetectContext();

        if (!VehicleTypeEnum.TRANSPORT.name().equals(plateDetectContext.getVehicleType())) {
            return;
        }

        //判断是抬杆还是落杆
        if (GIoInTypeEnum.UP.equals(plateDetectContext.getGIoInType())) {
            //如果是抬杆 保存车头照 后五秒保存车中照

            doEvent(plateDetectContext, VehicleSnapshotTypeEnum.HEAD);

            scheduler.schedule(() -> doEvent(plateDetectContext, VehicleSnapshotTypeEnum.MID), 5, TimeUnit.SECONDS);
        } else {
            doEvent(plateDetectContext, VehicleSnapshotTypeEnum.TAIL);

            // 上传政府
        }

    }



    public void doEvent(PlateDetectContext context, VehicleSnapshotTypeEnum vehicleSnapshotType) {
        //获取高位相机
        CameraEntity snapCameraEntity = cameraService.getCameraById(context.getCameraEntity().getAssociationCameraId());
        if (snapCameraEntity == null) {
            throw VehicleAccessException.DATE_NOT_EXIST.newInstance("获取高位相机失败 获取结果为空");
        }

        //获取最近的一张照片
        String url = cameraSnapshotService.getLastSnapshot(snapCameraEntity.getCameraIp(), new Date());

        try {
            byte[] bytes = Files.readAllBytes(Paths.get(url));
            String base64 = Base64ToImageUtil.bytesToBase64(bytes);
            url = Base64ToImageUtil.saveBase64(base64, Constants.PICTURE_FILE_LEDGER_PATH);
        } catch (IOException e) {
            log.error("图片文件读取异常", e);
        }
        //更新台账 放入照片地址
        vehicleTransportLedgerService.recordHeadMidTailImg(context.getVehicleTransportLedgerEntity().getId(), context.getInOutType(), vehicleSnapshotType, url);
    }
}
